package com.ruibang.glass.produce.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.utils.DateUtils;
import com.ruibang.glass.produce.domain.req.Hot2StatisticalReportReq;
import com.ruibang.glass.produce.domain.resp.hot2.Hot2ShiftStatisticsInfo;
import com.ruibang.glass.produce.entity.Hot2StatisticalReport;
import com.ruibang.glass.produce.mapper.Hot2StatisticalReportMapper;
import com.ruibang.glass.produce.service.Hot2StatisticalReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-05-27  15:06
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class Hot2StatisticalReportServiceImpl extends ServiceImpl<Hot2StatisticalReportMapper, Hot2StatisticalReport> implements Hot2StatisticalReportService {


    /**
     * @Description: 根据日期获取统计报表数据
     * @version v1.0
     * @author songJinKang
     * @date 2024-05-28 20:42
     */
    @Override
    public Map<Integer, Hot2StatisticalReport> getByDate(Hot2StatisticalReportReq hot2StatisticalReportReq) {
        LocalDate date = hot2StatisticalReportReq.getDate();
        LocalDateTime startT = LocalDateTime.of(date, LocalTime.of(8, 0));
        LocalDateTime endT = LocalDateTime.of(date.plusDays(1), LocalTime.of(7, 0));
        List<Hot2StatisticalReport> hot2StatisticalReportList = baseMapper.getByDate(startT, endT);
        Map<Integer, Hot2StatisticalReport> dataMap = new LinkedHashMap<>();
        if (date.compareTo(LocalDate.now()) == 0) {
            if (LocalDateTime.now().compareTo(startT) < 0) {
                return dataMap;
            }
            if (CollectionUtils.isNotEmpty(hot2StatisticalReportList)) {
                for (Hot2StatisticalReport hot2StatisticalReport : hot2StatisticalReportList) {
                    dataMap.put(hot2StatisticalReport.getCollectTime().getHour(), hot2StatisticalReport);
                }
                //统计当前时间数据（查询当前两个小时的最新数据）
                Hot2StatisticalReport hot2StatisticalReport = hot2StatisticalReportList.get(hot2StatisticalReportList.size() - 1);
                LocalDateTime lastCollectTime = hot2StatisticalReport.getCollectTime();
                long hour = Math.abs(DateUtils.calculateHourDifference(LocalDateTime.now(), lastCollectTime));
                getHourData(lastCollectTime, dataMap, hour);
            } else {
                //如果库中还没有数据，就查询实时数据
                long currentHourCal = Math.abs(DateUtils.calculateHourDifference(LocalDateTime.now(), startT));
                currentHourCal = currentHourCal == 0 ? 1 : currentHourCal + 1;
                LocalDateTime lastEntTime = null;
                for (long i = 1; i <= currentHourCal; i++) {
                    if (lastEntTime == null) {
                        lastEntTime = startT.plusHours(i);
                        log.error("start:{}   end:{}", startT, lastEntTime);
                        Hot2StatisticalReport data = this.executeHourTask(startT.toInstant(ZoneOffset.of("+8")).toEpochMilli(), lastEntTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());
                        dataMap.put(startT.getHour(), data);
                    } else {
                        LocalDateTime currentEndTime = lastEntTime.plusHours(1);
                        log.error("start:{}   end:{}", lastEntTime, currentEndTime);
                        Hot2StatisticalReport data = this.executeHourTask(lastEntTime.toInstant(ZoneOffset.of("+8")).toEpochMilli(), currentEndTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());
                        dataMap.put(lastEntTime.getHour(), data);
                        lastEntTime = currentEndTime;
                    }
                }
            }
        } else {
            for (Hot2StatisticalReport hot2StatisticalReport : hot2StatisticalReportList) {
                dataMap.put(hot2StatisticalReport.getCollectTime().getHour(), hot2StatisticalReport);
            }
        }
        return dataMap;
    }

    private void getHourData(LocalDateTime startT, Map<Integer, Hot2StatisticalReport> dataMap, long currentHourCal) {
        LocalDateTime lastEntTime = null;
        for (long i = 1; i <= currentHourCal; i++) {
            if (lastEntTime == null) {
                LocalDateTime startTime = startT.plusHours(i);
                lastEntTime = startTime.plusHours(i);
                log.error("start:{}   end:{}", startTime, lastEntTime);
                Hot2StatisticalReport data = this.executeHourTask(startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli(), lastEntTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());
                dataMap.put(startTime.getHour(), data);
            } else {
                LocalDateTime currentEndTime = lastEntTime.plusHours(1);
                log.error("start:{}   end:{}", lastEntTime, currentEndTime);
                Hot2StatisticalReport data = this.executeHourTask(lastEntTime.toInstant(ZoneOffset.of("+8")).toEpochMilli(), currentEndTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());
                dataMap.put(lastEntTime.getHour(), data);
                lastEntTime = currentEndTime;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.calculateHourDifference(LocalDateTime.now(), LocalDateTime.of(LocalDate.now(), LocalTime.of(15, 55))));


    }


//    @Override
//    public Hot2StatisticalReportResp getByDate(Hot2StatisticalReportReq hot2StatisticalReportReq) {
//        LocalDate date = hot2StatisticalReportReq.getDate();
//        List<Hot2StatisticalReport> hot2StatisticalReportList = baseMapper.getByDate(date);
//        Hot2StatisticalReportResp hot2StatisticalReportResp = new Hot2StatisticalReportResp();
//        if (CollectionUtils.isNotEmpty(hot2StatisticalReportList)) {
//            //统计白班 8点-19点
//
//
//            //统计晚班 20点-7点
//
//
//            //计算全天
//
//        }
//
//        return hot2StatisticalReportResp;
//    }


    /**
     * @Description: 处理白班数据
     * @version v1.0
     * @author songJinKang
     * @date 2024-05-28 19:45
     */
    private Hot2ShiftStatisticsInfo processDayShift(List<Hot2StatisticalReport> hot2StatisticalReportList, LocalDate date) {
        Hot2ShiftStatisticsInfo hot2ShiftStatisticsInfo = new Hot2ShiftStatisticsInfo();
        LocalDateTime startTime = LocalDateTime.of(date, LocalTime.of(8, 0));
        LocalDateTime endTime = LocalDateTime.of(date, LocalTime.of(19, 0));
        List<Hot2StatisticalReport> data = hot2StatisticalReportList.stream().filter(e -> e.getCollectTime().compareTo(startTime) >= 0 && e.getCollectTime().compareTo(endTime) <= 0).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(data)) {
            //统计小时数据
            Map<Integer, Hot2StatisticalReport> collectDataMap = new LinkedHashMap<>();
            for (Hot2StatisticalReport hot2StatisticalReport : data) {
                collectDataMap.put(hot2StatisticalReport.getCollectTime().getHour(), hot2StatisticalReport);
            }
            hot2ShiftStatisticsInfo.setData(collectDataMap);
            //总和数据
        }
        return hot2ShiftStatisticsInfo;
    }

    /**
     * @Description: 处理晚班数据
     * @version v1.0
     * @author songJinKang
     * @date 2024-05-28 19:45
     */
    private Hot2ShiftStatisticsInfo processEveningShift(List<Hot2StatisticalReport> hot2StatisticalReportList, LocalDate date) {
        LocalDateTime startTime = LocalDateTime.of(date, LocalTime.of(20, 0));
        LocalDateTime endTime = LocalDateTime.of(date, LocalTime.of(7, 0));

        return null;
    }

    /**
     * @Description: 热端生产统计、每小时任务
     * @version v1.0
     * @author songJinKang
     * @date 2024-05-28 10:50
     */
    @Override
    public Hot2StatisticalReport executeHourTask(long startTs, long endTs) {
        return baseMapper.getMesDetailData(String.valueOf(startTs), String.valueOf(endTs));
    }

}
